#include "widget.h"
#include "ui_widget.h"
#include <QtNetwork>

bool Widget::isFrame(QByteArray datagram)
{
    if(datagram[0]==0x02 && datagram[datagram.size()-1]==0x03)
        return true;
    else
        return false;
}

Widget::frameTypeEnum Widget::frameType(QByteArray frame)
{
    frameTypeEnum t;
    switch(frame[1])
    {
    case 0x30:
        t=order;
    }
    return t;
}

void Widget::frameHandle(frameTypeEnum Type, QByteArray datagram)
{
    int account = 0,tableNumber=0,row=0;
    QString s;
    QStringList sl1,sl2;
    QList<struct order*>::const_iterator i;
    sl1<<"桌号"<<"时间"<<"金额"<<"川粉带"<<"毛肚"<<"豆腐"<<"特色蘸料"<<"清油麻辣火锅"<<"羊排卷";
    sl2<<"时间"<<"金额"<<"川粉带"<<"毛肚"<<"豆腐"<<"特色蘸料"<<"清油麻辣火锅"<<"羊排卷";
    switch(Type)
    {
    case order:
        //解析数据
        struct order *tempOrder = new struct order;
        sscanf(datagram.data(),"\002\060zhuohao=%d jine=%d chuanfendai=%d doufu=%d maodu=%d qingyoumalahuoguo=%d yangpaijuan=%d tesezhanliao=%d\003",
               &tempOrder->zhuohao,&tempOrder->jine,&tempOrder->chuanfendain,&tempOrder->doufun,&tempOrder->maodun,&tempOrder->qingyoumalahuoguon,&tempOrder->yangpaijuann,&tempOrder->tesezhanliaon);
        tempOrder->time = QDateTime::currentDateTime().toString("h点m分s秒 yyyy年M月d日");
        orderL.append(tempOrder);
        //更新表格
        deleteTable();
        s=ui->comboBox_tableNumber->currentText();
        if(s=="全部")
        {
            ui->tableWidget->setHorizontalHeaderLabels(sl1);
            for(i=orderL.constBegin();i!=orderL.constEnd();i++)
            {
                addAnOrderToTableWithZhuoHao(row,*i);
                row++;
            }
            ui->label_account->clear();
        }
        else
        {
            ui->tableWidget->setHorizontalHeaderLabels(sl2);
            tableNumber = s.toInt();
            for(i=orderL.constBegin();i!=orderL.constEnd();i++)
                if((*i)->zhuohao == tableNumber)
                {
                    addAnOrderToTable(row,*i);
                    row++;
                }
            //更新总金额
            for(i=orderL.constBegin();i!=orderL.constEnd();i++)
                if( (*i)->zhuohao == tableNumber )
                    account += (*i)->jine;
            s.setNum(account);
            ui->label_account->setText("消费"+s+"元");
        }
        break;
    }
}

void Widget::addAnOrderToTable(int row, struct order * const o)
{
    QString s;

    ui->tableWidget->insertRow(row);

    itemPointerL[0] = new QTableWidgetItem(Widget::time);
    itemPointerL[0]->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    itemPointerL[0]-> setText(o->time);
    ui->tableWidget->setItem(row,Widget::columntime,itemPointerL[0]);

    itemPointerL[1] =  new QTableWidgetItem(Widget::jine);
    itemPointerL[1]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->jine);
    itemPointerL[1]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnjine,itemPointerL[1]);

    itemPointerL[2] =  new QTableWidgetItem(Widget::chuanfendain);
    itemPointerL[2]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->chuanfendain);
    itemPointerL[2]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnchuanfendai,itemPointerL[2]);

    itemPointerL[3] =  new QTableWidgetItem(Widget::maodun);
    itemPointerL[3]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->maodun);
    itemPointerL[3]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnmaodu,itemPointerL[3]);

    itemPointerL[4] =  new QTableWidgetItem(Widget::doufun);
    itemPointerL[4]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->doufun);
    itemPointerL[4]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columndoufu,itemPointerL[4]);

    itemPointerL[5] =  new QTableWidgetItem(Widget::tesezhanliaon);
    itemPointerL[5]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->tesezhanliaon);
    itemPointerL[5]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columntesezhanliao,itemPointerL[5]);

    itemPointerL[6] =  new QTableWidgetItem(Widget::qingyoumalahuoguon);
    itemPointerL[6]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->qingyoumalahuoguon);
    itemPointerL[6]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnqingyoumalahuoguo,itemPointerL[6]);

    itemPointerL[7] =  new QTableWidgetItem(Widget::columnyangpaijuann);
    itemPointerL[7]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->yangpaijuann);
    itemPointerL[7]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnyangpaijuan,itemPointerL[7]);
}

void Widget::addAnOrderToTableWithZhuoHao(int row, struct order * const o)
{
    QString s;

    ui->tableWidget->insertRow(row);

    itemPointerL[0] = new QTableWidgetItem(Widget::zhuohao);
    itemPointerL[0]->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->zhuohao);
    itemPointerL[0]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnzhuohao,itemPointerL[0]);

    itemPointerL[1] = new QTableWidgetItem(Widget::time);
    itemPointerL[1]->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    itemPointerL[1]-> setText(o->time);
    ui->tableWidget->setItem(row,Widget::columntime+1,itemPointerL[1]);

    itemPointerL[2] =  new QTableWidgetItem(Widget::jine);
    itemPointerL[2]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->jine);
    itemPointerL[2]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnjine+1,itemPointerL[2]);

    itemPointerL[3] =  new QTableWidgetItem(Widget::chuanfendain);
    itemPointerL[3]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->chuanfendain);
    itemPointerL[3]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnchuanfendai+1,itemPointerL[3]);

    itemPointerL[4] =  new QTableWidgetItem(Widget::maodun);
    itemPointerL[4]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->maodun);
    itemPointerL[4]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnmaodu+1,itemPointerL[4]);

    itemPointerL[5] =  new QTableWidgetItem(Widget::doufun);
    itemPointerL[5]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->doufun);
    itemPointerL[5]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columndoufu+1,itemPointerL[5]);

    itemPointerL[6] =  new QTableWidgetItem(Widget::tesezhanliaon);
    itemPointerL[6]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->tesezhanliaon);
    itemPointerL[6]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columntesezhanliao+1,itemPointerL[6]);

    itemPointerL[7] =  new QTableWidgetItem(Widget::qingyoumalahuoguon);
    itemPointerL[7]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->qingyoumalahuoguon);
    itemPointerL[7]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnqingyoumalahuoguo+1,itemPointerL[7]);

    itemPointerL[8] =  new QTableWidgetItem(Widget::columnyangpaijuann);
    itemPointerL[8]-> setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    s.setNum(o->yangpaijuann);
    itemPointerL[8]-> setText(s);
    ui->tableWidget->setItem(row,Widget::columnyangpaijuan+1,itemPointerL[8]);

}

void Widget::deleteTable()
{
    int rowCount,columnCount;
    rowCount = ui->tableWidget->rowCount();
    columnCount = ui->tableWidget->columnCount();
    if(rowCount==0 && columnCount==0)
        ;
    else
    {
        for(int i=0;i<rowCount;i++)
            for(int j=0;j<columnCount;j++)
            {
                QTableWidgetItem *t = ui->tableWidget->takeItem(i,j);
                delete t;
            }
    }
    ui->tableWidget->setRowCount(0);
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowState(Qt::WindowMaximized);

    this->setWindowTitle("订单管理");
    this->setWindowIcon(QIcon(":/new/prefix1/restaurant.ico"));

    QTableWidgetItem * t=nullptr;
    for(int i=1;i<=9;i++)
        itemPointerL.append(t);
    ui->comboBox_tableNumber->addItem("全部");
    for(int i=1;i<=100;i++)
        ui->comboBox_tableNumber->addItem(QString("%1").arg(i));
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setColumnWidth(1,190);

    udpSocket = new QUdpSocket(this);
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(do_udpSocket_readyRead()));
    quint16 port = 1557;
    udpSocket->bind(port);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::do_udpSocket_readyRead()
{
    while(udpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(),datagram.size());
        if(isFrame(datagram))
        {
            switch(frameType(datagram))
            {
            case order:
                frameHandle(order,datagram);
            }
        }
    }
}

void Widget::on_comboBox_tableNumber_currentTextChanged(const QString &arg1)
{
    int account = 0,tableNumber=0,row=0,flag=0;
    QString s;
    QStringList sl1,sl2;
    sl1<<"桌号"<<"时间"<<"金额"<<"川粉带"<<"毛肚"<<"豆腐"<<"特色蘸料"<<"清油麻辣火锅"<<"羊排卷";
    sl2<<"时间"<<"金额"<<"川粉带"<<"毛肚"<<"豆腐"<<"特色蘸料"<<"清油麻辣火锅"<<"羊排卷";
    QList<struct order*>::const_iterator i;
    //更新表格
    s=ui->comboBox_tableNumber->currentText();
    deleteTable();
    if(s=="全部")
    {
        ui->pushButton_settle->setEnabled(false);
        ui->tableWidget->setColumnCount(9);
        ui->tableWidget->setHorizontalHeaderLabels(sl1);
        ui->tableWidget->setColumnWidth(1,190);
        ui->tableWidget->setColumnWidth(0,90);

        for(i=orderL.constBegin();i!=orderL.constEnd();i++)
        {
            addAnOrderToTableWithZhuoHao(row,*i);
            row++;
        }
        ui->label_account->clear();
    }
    else
    {
        ui->pushButton_settle->setEnabled(true);
        ui->tableWidget->setColumnCount(8);
        ui->tableWidget->setHorizontalHeaderLabels(sl2);
        ui->tableWidget->setColumnWidth(0,190);
        ui->tableWidget->setColumnWidth(1,90);

        tableNumber = s.toInt();
        for(i=orderL.constBegin();i!=orderL.constEnd();i++)
            if((*i)->zhuohao == tableNumber)
            {
                addAnOrderToTable(row,*i);
                row++;
                flag++;
            }
        if(flag==0)
            ui->label_account->clear();
        else
        {
            //更新总金额
            for(i=orderL.constBegin();i!=orderL.constEnd();i++)
                if( (*i)->zhuohao == tableNumber )
                    account += (*i)->jine;
            s.setNum(account);
            ui->label_account->setText("消费"+s+"元");
        }
    }
}


void Widget::on_pushButton_settle_clicked()
{
    int tableNumber=0,index,number=0;
    QString s;
    //删除orderL订单列表中的相关数据
    QList<struct order*>::reverse_iterator i;
    s=ui->comboBox_tableNumber->currentText();
    tableNumber = s.toInt();
    for(i=orderL.rbegin();i!=orderL.rend();i++)
        if((*i)->zhuohao == tableNumber)
            number++;

    if(number != 0)
    {
        index=orderL.size();
        for(i=orderL.rbegin();i!=orderL.rend();i++)
        {
            index--;
            if((*i)->zhuohao == tableNumber)
            {
                if(index == 0){
                    delete *i;
                    orderL.removeAt(index);
                    break;
                }
                else{
                    delete *i;
                    orderL.removeAt(index);
                }
            }
        }
    }

    deleteTable();
    ui->label_account->clear();
}

